package com.tsd.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysSettingMapper;
import com.tsd.system.entity.SysSetting;
import com.tsd.system.entity.SysSettingExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysSettingService;
import com.tsd.core.utils.FieldUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysSetting 服务实现层
 *
 * @author david
 * @date 2021/07/15 10:35:26
 */
@Service
public class SysSettingServiceImpl extends BaseServiceImpl implements SysSettingService {

    @Resource
    private SysSettingMapper sysSettingMapper;

    @Override
    public SysSetting getSysSettingById(Long id) throws Exception {
        return sysSettingMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysSettingExt queryBySid(String sid) throws Exception {
        return sysSettingMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysSetting(Long id, SysUser opUser) throws Exception {
        SysSetting old = sysSettingMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        super.autoInjectBaseData(old, opUser, TYPE_DELETE);
        sysSettingMapper.updateByPrimaryKeySelective(old);
    }

    @Override
    public void saveSysSetting(SysSettingExt record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            SysSetting old = sysSettingMapper.selectByCodeOwnerSid(record);
            if (old != null) {
                record.setId(old.getId());
            }
        }
        if (HlpUtils.isEmpty(record.getId())) {
            super.checkEmpty(record.getCode(), "请输入编码");
            super.autoInjectBaseData(record, opUser, TYPE_CREATE);
            sysSettingMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, TYPE_UPDATE);
            sysSettingMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<SysSettingExt> findSysSettings(Map<String, Object> params, PageBean page) throws Exception {
        return sysSettingMapper.selectByPage(params, page);
    }

    @Override
    public List<SysSettingExt> findSysSettings(Map<String, Object> params) throws Exception {
        return sysSettingMapper.selectByParams(params);
    }

    @Override
    public SysSettingExt getSysSettingByCodeOwnerSid(SysSettingExt query) throws Exception {
        return sysSettingMapper.selectByCodeOwnerSid(query);
    }

    @Override
    public <T> T queryValidSettingByCode(String code, Class<T> clazz) throws Exception {
        SysSettingExt query = new SysSettingExt();
        query.setCode(code);
        query.setDr(0);
        SysSettingExt sysSetting = sysSettingMapper.selectByCodeOwnerSid(query);
        if (sysSetting != null) {
            T setting = JSON.parseObject(sysSetting.getJson(), clazz);
            FieldUtils.setFieldValue(setting, "sys_setting_sid", sysSetting.getSid(), true);
            return setting;
        }
        return null;
    }
}
